<template>
  <div class="department-container">
    <el-tree :data="depts" :props="defaultProps" default-expand-all>
      <template v-slot="{data}">
        <el-row style="width:100%;">
          <el-col :span="18"><div>{{ data.name }}</div></el-col>
          <el-col :span="6">
            <div style="background:pink;width:200px;" @click.stop="">
              <span class="identity">{{ data.managerName }}</span>
              <!-- 下拉框事件 -->
              <el-dropdown trigger="click" @command="dropdownTem($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <AddEditDialog :isEdit="isEdit" :isShow.sync="isShowDialog" :departmentList="departmentList" :currentId="currentId" @update-list="getDepartmentList" />
  </div>
</template>

<script>
import { companyDepartmentRequest, delDepartmentRequest } from '@/api/department'
import { transformListToTree } from '@/utils/index'
import AddEditDialog from './components/addEditDialog.vue'
export default {
  name: 'Department',
  components: {
    AddEditDialog
  },
  data() {
    return {
      depts: [],
      departmentList: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      isShowDialog: false,
      currentId: '',
      isEdit: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门列表方法
    async getDepartmentList() {
      const res = await companyDepartmentRequest()
      this.departmentList = res.data
      this.depts = transformListToTree(res.data, 0)
    },
    // 下拉框事件
    async dropdownTem(command, id) {
      this.currentId = id
      if (command === 'add') { // 新增
        this.isEdit = false
        this.isShowDialog = true
      } else if (command === 'delete') { // 删除
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 发送请求
          await delDepartmentRequest(id)
          this.$message.success('删除成功')
          // 刷新列表
          this.getDepartmentList()
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      } else { // 修改
        this.isEdit = true
        this.isShowDialog = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
   .department-container{
    padding: 30px;
    background-color: #e1dbdb;
    ::v-deep .el-tree{
      // background-color: #3aae66;
      // height: 100vh;
      .identity{
        margin-right: 20px;
        text-align: right;
        background-color: #e17b7b;
      }
      /* .el-dropdown-link{
        text-align: right;
      } */
    }
  }
</style>
